<?php

namespace app\opengzh\service;




use app\common\model\Article;
use app\common\model\GzhDeepseekLog;
use app\common\model\GzhTitle;
use GuzzleHttp\Client;
use think\facade\Db;

class DeepSeekService  {

    //csgzhkey
    protected $apiKey= 'sk-4a4b731a27cb4d5c9e744b327e7297c0';
    protected $baseUrl = 'https://api.deepseek.com/chat/completions'; // 替换为实际API地址

    public function __construct()
    {
    }

    public function sendRequest($data = [])
    {
        $articleTitle = '《'.trim($data['title']).'》'.$data['keyword'];
        if($data['keyword']=='超人气版本新服！新服狂欢，上线即送顶赞！（附特权礼包码）'){
            $articleTitle = '《'.trim($data['title']).'》散人微氪轻松过秒大佬攻略！（附特权礼包码';
        }
        $client = new Client([
            'timeout'  => 100.0,
        ]);
        $response = $client->post($this->baseUrl,[
            'json'=>[

                'messages'=>[
                    [
                        "content"=>"帮我写".$articleTitle."，请只要写攻略就好。限500字内，请一定要原创",
                        "role"=> "user"
                    ]
                ],
                "model"=> "deepseek-chat",
                "frequency_penalty"=> 0,
                "max_tokens"=> 2048,
                "presence_penalty"=> 0,
                "response_format"=> ["type"=> "text"],
                "stop"=> null,
                "stream"=> false,
                "stream_options"=> null,
                "temperature"=> 1.5,
                "top_p"=> 1,
                "tools"=>null,
                "tool_choice"=> "none",
                "logprobs"=> false,
                "top_logprobs"=> null

            ],
            'headers'=>[
                'Authorization' => 'Bearer ' . $this->apiKey,
                'Content-Type' => 'application/json',
            ]
        ]);
        $str = $response -> getBody()->getContents();
        $arr = json_decode($str,true);
        $dslogModel = new GzhDeepseekLog();
        $dslogModel->insert([
            'title'=> $articleTitle,
            'title_id'=> $data['id'],
            'result'=> $str,
            'created_at'=> time(),
        ]);
        if(isset($arr['id'])){

            $content = $arr['choices'][0]['message']['content'];
            $specifiedChar1 = '#';
            $specifiedChar2 = '*';
            $specifiedChar3 = ' ';
            $content= str_replace($specifiedChar1, "", $content);
            $content= str_replace($specifiedChar2, "", $content);
            $content= str_replace($specifiedChar3, "", $content);
            $paragraphs = explode("\n", $content);
            $content2 = '';
            foreach ($paragraphs as $paragraph) {
                $content2 = $content2.$paragraph.'<br>';
            }

            $articleModel = new Article();
            $articleModel->insert([
                'title'=>$articleTitle,
                'content'=>$arr['choices'][0]['message']['content'],
                'content2'=>$content2,
                'title_id'=>$data['id'],
                'created_at'=>time(),
            ]);
            $titleModel = new GzhTitle();
            $titleModel->where('id',$data['id'])->save([
                'is_article'=>2,
            ]);
        }
        var_dump('请求完成');
    }

}

